package com.stone.business.tabl.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.stone.business.tabl.pojo.TTablDz;
import com.stone.business.tabl.pojo.TTablJk;
import com.stone.business.tabl.pojo.TTablXsb;
import com.stone.business.tabl.pojo.TTablYb;
import com.stone.common.pojo.TFile;
import com.stone.sys.pojo.TOrgan;

import frm.ssh.exception.AppException;
import frm.ssh.service.BaseService;

public class TablService extends BaseService {
	/**
	 * 根据ID查询记录
	 * 
	 * @param id ID
	 * @param objectFullName POJO名 包括包路径
	 * @return Object
	 */
	public Object findById(Long id, String objectFullName)  {
		return super.findById(id, objectFullName);
	}
	
	/**
	 * 保存pojo对象
	 * 
	 * @param transientInstance pojo对象
	 */
	public void save(Object transientInstance)  {
			super.save(transientInstance);
	}
	
	/**
	 * 修改记录
	 * 
	 * @param transientInstance pojo对象
	 */
	public void update(Object transientInstance)  {
			super.update(transientInstance);
	}
	
	/**
	 * 修改记录
	 * 
	 * @param transientInstance pojo对象
	 */
	public void doDelAll(final String ids,final String objectSimpleName)  {
			super.doDelAll(ids,objectSimpleName);
	}
	
	/**
	 * 通过SQL查询列表，返回每条是一个HashMap<String,Object>
	 * @param sql 查询SQL语句
	 * @return list 返回列表
	 */
	public List<HashMap<String,Object>> findListBySql(String sql){
	
		List<HashMap<String,Object>> list =  (List<HashMap<String,Object>>) super.findListBySql(sql);
		return list;
	}

	
	/**
	 * 此方法是用来动态获取列表，可以多表关联
	 * 
	 * @param cols 查询列名
	 * @param conditions 查询条件
	 * @param vals 查询条件对应值
	 * @return clause order by, group by,having等子句
	 * @param objectSimpleName 
	 */
	public List getListByDHql(final String cols, final String conditions,
			final Object[] vals, final String clause,
			final String objectSimpleName)  {
		
		return super.getListByDHql(cols, conditions, vals, clause, objectSimpleName);
	}
	
	/**
	 * 根据ID删除记录
	 * 
	 * @param id 删除记录的ID
	 * @param objectSimpleName
	 */
	public void delete(Long id, String objectSimpleName){
			super.delete(id, objectSimpleName);
	}
	/**
	 * 通过HQL修改记录
	 * 
	 * @param hql 修改的HQL语句
	 * @param values HQL语句对应值
	 */
	public void updateByHQL(String hql, Object[] values){
		super.updateByHQL(hql,values);
	}
	/**
	 * 查询一条记录
	 * @return
	 * @throws AppException
	 */	
	public String findListOrgan(String organname)  throws IOException  {
		
		List<TOrgan> l = null;
		l = this.findListByHql("from TOrgan where organName = '" + organname + "'");
       return l.get(0).getOrganCode()+"";
		
	}
	
	public String doSave(TTablXsb entity, ArrayList<TFile> fileList) {
		if (entity.getId() == 0) {
			super.save(entity);
			if (fileList != null && fileList.size() > 0) {
				for(int i = 0; i < fileList.size(); i++){
					TFile file = fileList.get(i);
					file.setTableid(entity.getId());
					super.save(file);
				}
			}
		} else {
			if (fileList != null && fileList.size() > 0) {
				for(int i = 0; i < fileList.size(); i++){
					TFile file = fileList.get(i);
					super.save(file);
				}
			}
			super.update(entity);

		}
		return "保存成功。";
	}
	
	public String doDzSave(TTablDz entity, ArrayList<TFile> fileList) {
		if (entity.getId() == 0) {
			super.save(entity);
			if (fileList != null && fileList.size() > 0) {
				for(int i = 0; i < fileList.size(); i++){
					TFile file = fileList.get(i);
					file.setTableid(entity.getId());
					super.save(file);
				}
			}
		} else {
			if (fileList != null && fileList.size() > 0) {
				for(int i = 0; i < fileList.size(); i++){
					TFile file = fileList.get(i);
					super.save(file);
				}
			}
			super.update(entity);

		}
		return "保存成功。";
	}
	
	public String doYbSave(TTablYb entity, ArrayList<TFile> fileList) {
		if (entity.getId() == 0) {
			super.save(entity);
			if (fileList != null && fileList.size() > 0) {
				for(int i = 0; i < fileList.size(); i++){
					TFile file = fileList.get(i);
					file.setTableid(entity.getId());
					super.save(file);
				}
			}
		} else {
			if (fileList != null && fileList.size() > 0) {
				for(int i = 0; i < fileList.size(); i++){
					TFile file = fileList.get(i);
					super.save(file);
				}
			}
			super.update(entity);

		}
		return "保存成功。";
	}

	public String doJkSave(TTablJk entity, ArrayList<TFile> fileList) {
		if (entity.getId() == 0) {
			super.save(entity);
			if (fileList != null && fileList.size() > 0) {
				for(int i = 0; i < fileList.size(); i++){
					TFile file = fileList.get(i);
					file.setTableid(entity.getId());
					super.save(file);
				}
			}
		} else {
			if (fileList != null && fileList.size() > 0) {
				for(int i = 0; i < fileList.size(); i++){
					TFile file = fileList.get(i);
					super.save(file);
				}
			}
			super.update(entity);

		}
		return "保存成功。";
	}
}
